package com.wzq.blog.back.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzq.blog.back.bean.Article;
import com.wzq.blog.back.bean.Category;
import com.wzq.blog.back.bean.Tag;
import com.wzq.blog.back.bean.User;
import com.wzq.blog.back.service.ArticleService;
import com.wzq.blog.base.bean.ResultVo;
import com.wzq.blog.base.exception.BlogException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Enumeration;
import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 异步查询文章
     * @param page
     * @param pageSize
     * @param title
     * @param session
     * @return
     */
    @RequestMapping("/article/list")
    @ResponseBody
    public PageInfo<Article> list(int page, int pageSize, String title, HttpSession session){
        //获取当前用户
        User user = (User) session.getAttribute("user");

        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleService.list(user.getUid(),title);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        //List<Article> list = pageInfo.getList();
        //System.out.println(list);
        //System.out.println("pageInfo--->" + pageInfo);
        return pageInfo;
    }

    /**
     * 异步修改文章是否公开
     * @param article
     * @return
     */
    @RequestMapping("/article/isOpen")
    @ResponseBody
    public ResultVo isOpen(Article article){
        ResultVo resultVo = new ResultVo();
        try {
            articleService.isOpen(article);
            resultVo.setOk(true);
            if(article.getIsOpen().equals("0")){
                resultVo.setMess("文章已私密");
            }else{
                resultVo.setMess("文章已公开");
            }
        }catch (BlogException e){
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

    /**
     * 异步查询文章分类
     * @return
     */
    @RequestMapping("/article/queryCategory")
    @ResponseBody
    public ResultVo queryCategory(){
        ResultVo resultVo = new ResultVo();
        List<Category> categories = articleService.queryCategory();
        resultVo.setOk(true);
        resultVo.setMess("查询成功");
        resultVo.setT(categories);
        return resultVo;
    }

    /**
     * 查询栏目下的标签
     * @param cid
     * @return
     */
    @RequestMapping("/article/queryTags")
    @ResponseBody
    public List<Tag> queryTags(String cid){
        List<Tag> tags = articleService.queryTags(cid);
        return tags;
    }

    /**
     * 异步发布文章
     * @param article
     * @param session
     * @return
     */
    @RequestMapping("/article/saveOrUpdate")
    @ResponseBody
    public ResultVo saveOrUpdate(Article article,HttpSession session){
        ResultVo resultVo = new ResultVo();
        try {
            User user = (User) session.getAttribute("user");
            article.setUid(user.getUid());
            article = articleService.saveOrUpdate(article);
            resultVo.setOk(true);
            if(article.getAid() == null){
                resultVo.setMess("文章发布成功");
            }else {
                resultVo.setMess("修改文章成功");
            }
            resultVo.setT(article);
        } catch (BlogException e) {
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

    /**
     * 异步查询文章信息
     * @param id
     * @return
     */
    @RequestMapping("/article/queryById")
    @ResponseBody
    public Article queryById(String id){
        System.out.println("id=============================------>"+id);
        Article article = articleService.queryById(id);
        return article;
    }

    @RequestMapping("/article/deleteById")
    @ResponseBody
    public ResultVo deleteById(String id){
        System.out.println("id=============================------>"+id);
        ResultVo resultVo = new ResultVo();
        try {
            articleService.deleteById(id);
            resultVo.setOk(true);
            resultVo.setMess("删除成功!");
        } catch (Exception e) {
            resultVo.setOk(false);
            resultVo.setMess("删除失败！");
        }
        return resultVo;
    }
}
